<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>签到配置管理</title>
    <style>
        body {
            font-family: "Microsoft YaHei", sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            color: #666;
        }
        input, select {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            padding: 10px 20px;
            background-color: #4285f4;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #3367d6;
        }
        .error {
            color: #e53935;
            text-align: center;
            margin-top: 10px;
        }
        .sign-list {
            margin-top: 30px;
        }
        .sign-item {
            border: 1px solid #ddd;
            padding: 15px;
            margin-bottom: 15px;
            border-radius: 4px;
        }
        .sign-item button {
            background-color: #e53935;
        }
        .sign-item button:hover {
            background-color: #c62828;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>签到配置管理</h1>
        
        <form id="signForm">
            <div class="form-group">
                <label for="signName">签到名称</label>
                <input type="text" id="signName" required>
            </div>
            <div class="form-group">
                <label for="signInId">签到ID</label>
                <input type="text" id="signInId" required>
            </div>
            <div class="form-group">
                <label for="startTime">开始时间</label>
                <input type="datetime-local" id="startTime" required>
            </div>
            <div class="form-group">
                <label for="endTime">结束时间</label>
                <input type="datetime-local" id="endTime" required>
            </div>
            <div class="form-group">
                <label for="refreshInterval">刷新间隔(秒)</label>
                <input type="number" id="refreshInterval" value="5" min="1" max="60" required>
            </div>
            <button type="submit">添加签到</button>
        </form>

        <div class="sign-list">
            <h2>已配置的签到</h2>
            <div id="signList"></div>
        </div>
    </div>

    <script>
        // 检查登录状态
        function checkAuth() {
            const token = localStorage.getItem('adminToken');
            if (!token) {
                window.location.href = '/admin_login.html';
            }
        }

        // 加载签到列表
        async function loadSignList() {
            try {
                const response = await fetch('/api/admin/signs', {
                    headers: {
                        'Authorization': `Bearer ${localStorage.getItem('adminToken')}`
                    }
                });
                const data = await response.json();
                
                if (data.success) {
                    const signList = document.getElementById('signList');
                    signList.innerHTML = '';
                    
                    data.signs.forEach(sign => {
                        const signItem = document.createElement('div');
                        signItem.className = 'sign-item';
                        signItem.innerHTML = `
                            <h3>${sign.name}</h3>
                            <p>签到ID: ${sign.signInId}</p>
                            <p>开始时间: ${new Date(sign.startTime).toLocaleString()}</p>
                            <p>结束时间: ${new Date(sign.endTime).toLocaleString()}</p>
                            <p>刷新间隔: ${sign.refreshInterval}秒</p>
                            <button onclick="deleteSign('${sign.id}')">删除</button>
                        `;
                        signList.appendChild(signItem);
                    });
                }
            } catch (error) {
                console.error('加载签到列表失败:', error);
            }
        }

        // 删除签到
        async function deleteSign(id) {
            if (!confirm('确定要删除这个签到配置吗？')) return;
            
            try {
                const response = await fetch(`/api/admin/signs/${id}`, {
                    method: 'DELETE',
                    headers: {
                        'Authorization': `Bearer ${localStorage.getItem('adminToken')}`
                    }
                });
                const data = await response.json();
                
                if (data.success) {
                    loadSignList();
                } else {
                    alert(data.message || '删除失败');
                }
            } catch (error) {
                console.error('删除签到失败:', error);
                alert('删除失败，请重试');
            }
        }

        // 页面加载时检查认证并加载签到列表
        document.addEventListener('DOMContentLoaded', () => {
            checkAuth();
            loadSignList();
        });

        // 处理表单提交
        document.getElementById('signForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const formData = {
                name: document.getElementById('signName').value,
                signInId: document.getElementById('signInId').value,
                startTime: new Date(document.getElementById('startTime').value).getTime(),
                endTime: new Date(document.getElementById('endTime').value).getTime(),
                refreshInterval: parseInt(document.getElementById('refreshInterval').value)
            };
            
            try {
                const response = await fetch('/api/admin/signs', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': `Bearer ${localStorage.getItem('adminToken')}`
                    },
                    body: JSON.stringify(formData)
                });
                
                const data = await response.json();
                
                if (data.success) {
                    alert('添加成功');
                    document.getElementById('signForm').reset();
                    loadSignList();
                } else {
                    alert(data.message || '添加失败');
                }
            } catch (error) {
                console.error('添加签到失败:', error);
                alert('添加失败，请重试');
            }
        });
    </script>
</body>
</html> 